
#    This file is part of pyCorreios.
#    
#    pyCorreios is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#    
#    pyCorreios is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    
#    You should have received a copy of the GNU General Public License
#    along with pyCorreios.  If not, see <http://www.gnu.org/licenses/>.

#
# IMPORTS
#
import urllib2
import htmlparser
import time

#
# CONSTANTS
#
NACIONALURL = "http://websro.correios.com.br/sro_bin/txect01$.QueryList?P_LINGUA=001&P_TIPO=001&P_COD_UNI=%s"
RETRIES = 3

# 
# CODE
#
def retrieveNacional(trackingcode):
    """
    Go to the tracking site and download the entire page. Then parse it.
    """
    tries = RETRIES
    while tries > 0:
        try:
            fp = urllib2.urlopen(NACIONALURL % trackingcode)
            content = fp.read()
            fp.close()
            
            data = htmlparser.parseNacional(content)
            
            return data
        except:
            tries = tries - 1
            time.sleep(0.1)
# retrieveNacional()
